package com.sec.etech.form.cust.gencode;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.openbpm.base.api.constant.ColumnType;
import org.openbpm.base.core.util.StringUtil;
import org.openbpm.bus.api.constant.BusTableRelType;
import org.openbpm.bus.api.model.IBusTableRel;
import org.openbpm.bus.api.model.IBusTableRelFk;
import org.openbpm.bus.api.model.IBusinessColumn;
import org.springframework.stereotype.Component;

@Component
public class EtechGenCodeUtil {
  public String getUpperFk(IBusTableRel rel, boolean isFrom) { return StringUtil.upperFirst(getFkName(rel, isFrom)); }

  public String getFkName(IBusTableRel childRel, boolean isParentColumn) {
    if (childRel.getFks() != null)
      for (IBusTableRelFk fk : childRel.getFks()) {
        if (fk.getType().equals("parentField") || fk.getType().equals("childField"))
          return isParentColumn ? fk.getFrom() : fk.getValue();
      }
    return "";
  }

  public boolean isInversionFk(IBusTableRel childRel) {
    for (IBusTableRelFk fk : childRel.getFks()) {
      if (fk.getType().equals("childField"))
        return true;
    }
    return false;
  }

  public String upperFirst(String key) { return StringUtil.upperFirst(key); }

  public String getChildClassName(IBusTableRel child) {
    if (child.getType().equals(BusTableRelType.ONE_TO_MANY.getKey()))
      return String.format("List<%s>", new Object[] { StringUtil.upperFirst(child.getTableKey()) });
    return StringUtil.upperFirst(child.getTableKey());
  }

  public String getLowFirstChildClassKey(IBusTableRel child, boolean isInit) {
    String classKey = child.getTableKey();
    if (child.getType().equals(BusTableRelType.ONE_TO_MANY.getKey())) {
      classKey = classKey + "List";
      if (isInit)
        classKey = classKey + " = new ArrayList<>()";
    } else if (isInit) {
      classKey = classKey + " = null ";
    }
    return StringUtil.lowerFirst(classKey);
  }

  public String getUpperFirstChildClassKey(IBusTableRel child) { return StringUtil.upperFirst(getLowFirstChildClassKey(child, false)); }

  public String getColumnJavaType(IBusinessColumn column) {
    if (column.getType().equals(ColumnType.DATE.getKey()))
      return "Date";
    if (column.getType().equals(ColumnType.NUMBER.getKey())) {
      if (column.getDecimal() == 0){
        //active/isxx/needxx/havexxxx
        if(column.getLength()<=3){
          if("active".equalsIgnoreCase(column.getName())
            || "enable".equalsIgnoreCase(column.getName())
            || column.getName().startsWith("is")
                  || column.getName().startsWith("have")
                  || column.getName().startsWith("need")){
            return "Boolean";
          }
        }
        return (column.getLength() < 10) ? "Integer" : "Long";
      }

      return "BigDecimal";
    }
    return "String";
  }

  public String getColumnJdbcType(IBusinessColumn column) {
    if (column.getType().equals(ColumnType.DATE.getKey()))
      return "TIMESTAMP";
    if (column.getType().equals(ColumnType.NUMBER.getKey()))
      return "NUMERIC";
    return "VARCHAR";
  }

  public String getFkDbColumnName(IBusTableRel childRel) {
    if (childRel.getFks() != null)
      for (IBusTableRelFk fk : childRel.getFks()) {
        if (fk.getType().equals("parentField")) {
          IBusinessColumn fkColumn = childRel.getTable().getColumnByKey(fk.getFrom());
          return fkColumn.getName();
        }
      }
    return "";
  }

  public String getColumnFormatter(IBusinessColumn column) {
    if (column.getType().equals(ColumnType.DATE.getKey())) {
      String configStr = column.getCtrl().getConfig();
      if (StringUtil.isNotEmpty(configStr)) {
        String format = JSON.parseObject(configStr).getString("format");
        if ("yyyy-MM-dd".equals(format))
          return "data-formatter=\"dateFormatter\"";
      }
      return "data-formatter=\"dateTimeFormatter\"";
    }
    if ("select,radio".indexOf(column.getCtrl().getType()) != -1) {
      JSONObject config = JSON.parseObject(column.getCtrl().getConfig());
      if (!config.containsKey("options"))
        return "";
      JSONArray options = config.getJSONArray("options");
      StringBuilder formatStr = new StringBuilder(" data-formatter=\"labelFormatter\" ");
      JSONObject json = new JSONObject();
      for (int i = 0; i < options.size(); i++) {
        JSONObject option = options.getJSONObject(i);
        json.put(option.getString("key"), option.getString("txt"));
      }
      formatStr.append(" data-val-text='").append(json.toJSONString()).append("' ");
      return formatStr.toString();
    }
    return "";
  }

  public String getColumnIviewFormatter(IBusinessColumn column) {
    if (column.getType().equals(ColumnType.DATE.getKey())) {
      String configStr = column.getCtrl().getConfig();
      if (StringUtil.isNotEmpty(configStr)) {
        String format = JSON.parseObject(configStr).getString("format");
        return "date-format=\"" + format + "\"";
      }
      return "date-format=\"yyyy-MM-dd HH:mm\"";
    }
    if ("select,radio".indexOf(column.getCtrl().getType()) != -1) {
      JSONObject config = JSON.parseObject(column.getCtrl().getConfig());
      if (!config.containsKey("options"))
        return "";
      JSONArray options = config.getJSONArray("options");
      StringBuilder formatStr = new StringBuilder();
      JSONObject json = new JSONObject();
      for (int i = 0; i < options.size(); i++) {
        JSONObject option = options.getJSONObject(i);
        json.put(option.getString("key"), option.getString("txt"));
      }
      formatStr.append(" val-text='").append(json.toJSONString()).append("' ");
      return formatStr.toString();
    }
    return "";
  }
}
